package com.zfast.config.controller;

import com.zfast.config.constant.SessionKeyConst;
import com.zfast.config.entity.Menu;
import com.zfast.config.entity.User;
import com.zfast.config.service.TreeMenuService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Classname ManagerController
 * @Description 后台控制器
 * @Date 2019/7/16 14:43
 * @Created by WDD
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Resource
    private TreeMenuService treeMenuService;

    /**
     * 跳转后台页面
     *
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "manager/index";
    }

    /**
     * 异步加载权限树
     *
     * @param session
     * @return
     */
    @PostMapping("/treeMenu")
    @ResponseBody
    public Object treeMenu(HttpSession session) {
        if (!StringUtils.isEmpty(session.getAttribute(SessionKeyConst.TREEMENU))) {
            return session.getAttribute(SessionKeyConst.TREEMENU);
        }
        User user = (User) session.getAttribute(SessionKeyConst.USER);
        List<Menu> menuList = treeMenuService.selectByAdminId(user.getId());
        session.setAttribute(SessionKeyConst.TREEMENU, menuList);
        return menuList;
    }

    /**
     * 异步加载后台主页
     *
     * @return
     */
    @GetMapping("/console")
    public String console() {
        return "manager/console";
    }

}
